//
//  ManageAddressCell.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/15.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

class ManageAddressCell: UITableViewCell {

    // MARK: - life cycle
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initBaseLayout()
        layoutPageSubviews()
        self.selectionStyle = .none
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - event response
    func selectedAction(sender: UIButton) {
        if !sender.isSelected {
            sender.isSelected = true
        } else {
            sender.isSelected = false
        }
    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.addSubview(nameLabel)
        self.addSubview(addressImageView)
        self.addSubview(addressLabel)
        self.addSubview(lineView)
        self.addSubview(selectedBtn)
        self.addSubview(defaultAddress)
        self.addSubview(editBtn)
        self.addSubview(deletBtn)
    }
    
    func layoutPageSubviews() {
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(32)
            make.top.equalTo(25)
            make.width.equalTo(150)
            make.height.equalTo(12)
        }
        
        addressImageView.snp.makeConstraints { (make) in
            make.left.equalTo(14)
            make.top.equalTo(nameLabel.snp.bottom).offset(11)
            make.width.equalTo(11)
            make.height.equalTo(15)
        }
        
        addressLabel.snp.makeConstraints { (make) in
            make.left.equalTo(addressImageView.snp.right).offset(6)
            make.top.equalTo(nameLabel.snp.bottom).offset(8)
            make.right.equalTo(self).offset(-43)
            make.height.equalTo(34)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(4)
            make.top.equalTo(addressLabel.snp.bottom).offset(16)
            make.right.equalTo(self).offset(0)
            make.height.equalTo(1)
        }
        
        selectedBtn.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(5)
            make.top.equalTo(lineView.snp.bottom).offset(4)
            make.width.height.equalTo(32)
        }
        
        defaultAddress.snp.makeConstraints { (make) in
            make.left.equalTo(selectedBtn.snp.right).offset(0)
            make.top.equalTo(lineView.snp.bottom).offset(12)
            make.width.equalTo(79)
            make.height.equalTo(17)
        }
        
        editBtn.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-80)
            make.top.equalTo(lineView.snp.bottom).offset(0)
            make.width.equalTo(64)
            make.height.equalTo(44)
        }
        
        deletBtn.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-14)
            make.top.equalTo(lineView.snp.bottom).offset(0)
            make.width.equalTo(64)
            make.height.equalTo(44)
        }
    }
    
    func configUI() {
        nameLabel.text = "陈妞妞  "+"18832891210"
        addressLabel.text = "北京北京市朝阳区通惠河畔惠河南街1106号源创空间大厦108室分豆教育"
        
    }
    
    // MARK: - setters and getters
    var _nameLabel: UILabel!
    var nameLabel: UILabel {
        if _nameLabel == nil {
            _nameLabel = UILabel()
            _nameLabel.font = UIFont(name: "PingFangSC-Medium", size: 14)
            _nameLabel.textColor = UIColor(hexString: "#252525")
        }
        return _nameLabel
    }
    
    var _addressImageView: UIImageView!
    var addressImageView: UIImageView {
        if _addressImageView == nil {
            _addressImageView = UIImageView()
            _addressImageView.image = UIImage(named: "icon_address")
        }
        return _addressImageView
    }
    
    var _addressLabel: UILabel!
    var addressLabel: UILabel {
        if _addressLabel == nil {
            _addressLabel = UILabel()
            _addressLabel.font = UIFont(name: "PingFangSC-Regular", size: 12)
            _addressLabel.textColor = UIColor(hexString: "#666666")
            _addressLabel.numberOfLines = 2
        }
        return _addressLabel
    }
    
    var _lineView: UIView!
    var lineView: UIView {
        if _lineView == nil {
            _lineView = UIView()
            _lineView.backgroundColor = UIColor(hexString: "#D3D3D3")
        }
        return _lineView
    }
    
    var _selectedBtn: UIButton!
    var selectedBtn: UIButton {
        if _selectedBtn == nil {
            _selectedBtn = UIButton()
            _selectedBtn.setBackgroundImage(UIImage(named: "icon_select_n"), for: .normal)
            _selectedBtn.setBackgroundImage(UIImage(named: "icon_select_h"), for: .highlighted)
            _selectedBtn.setBackgroundImage(UIImage(named: "icon_select_s"), for: .selected)
            _selectedBtn.addTarget(self, action: #selector(ManageAddressCell.selectedAction(sender:)), for: .touchUpInside)
        }
        return _selectedBtn
    }
    
    var _defaultAddress: UILabel!
    var defaultAddress: UILabel {
        if _defaultAddress == nil {
            _defaultAddress = UILabel()
            _defaultAddress.font = UIFont(name: "PingFangSC-Regular", size: 12)
            _defaultAddress.textColor = UIColor(hexString: "#666666")
            _defaultAddress.text = "设为默认地址"
        }
        return _defaultAddress
    }
    
    var _editBtn: UIButton!
    var editBtn: UIButton {
        if _editBtn == nil {
            _editBtn = UIButton()
            _editBtn.setBackgroundImage(UIImage(named: "icon_edit_address_n"), for: .normal)
            _editBtn.setBackgroundImage(UIImage(named: "icon_edit_address_h"), for: .highlighted)
        }
        return _editBtn
    }
    
    var _deletBtn: UIButton!
    var deletBtn: UIButton {
        if _deletBtn == nil {
            _deletBtn = UIButton()
            _deletBtn.setBackgroundImage(UIImage(named: "icon_delete_address_n"), for: .normal)
            _deletBtn.setBackgroundImage(UIImage(named: "icon_delete_address_h"), for: .highlighted)
        }
        return _deletBtn
    }

}
